<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('helper.inc');

$headline = 26;
$drm = array(true, false, false, false, false, true, false);

require_once('menu.inc');

function ShowWDONE() {
	global $datetime;
	global $leneinheit;

	$db = OpenDB();
	$query = "select wd_num, wd_prnum, mi_nname, mi_vname, wd_datum, ";
	$query .= "wd_hours, ta_name, wd_task from wdone, mitarbeiter, task where ";
	$query .= "mi_num = wd_minum and ta_num = wd_task ";
	$query .= "order by wd_prnum, mi_nname, mi_vname, wd_task, wd_datum desc";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;

	if ($anz <= 0) {
	   closeDB($db);
	   Error("Es gibt keine Datens&auml;tze zum Anzeigen!");
	   return;
	}
?>
<table class="sel">
   <tr>
      <th class="sel">Lfd.</th>
      <th class="sel">PrNr.</th>
      <th class="sel">Mitarbeiter</th>
      <th class="sel">Datum / Zeit</th>
      <th class="sel">Stunden</th>
      <th class="sel">TaNum.</th>
      <th class="sel">Taskname</th>
   </tr>
<?
	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $wd_num = $data[0];
	   $wd_prnum = $data[1];
	   $mi_nname = $data[2];
	   $mi_vname = $data[3];
	   $wd_datum = $data[4];
	   $wd_hours = $data[5];
	   $ta_name = $data[6];
	   $ta_num = $data[7];
	   echo "<tr>\n<td class=\"selnum\">$wd_num</td>\n";
	   echo "<td class=\"selnum\">$wd_prnum</td>\n";
	   echo "<td class=\"sel\">$mi_nname $mi_vname</td>\n";
	   echo "<td class=\"selnum\">" . gmdate($datetime, $wd_datum) . "</td>\n";
	   echo "<td class=\"selnum\">" . FormatNum($wd_hours, 3) . " (PT: " . FormatNum($wd_hours / $leneinheit, 3) . ")</td>\n";
	   echo "<td class=\"selnum\">$ta_num</td>\n";
	   echo "<td class=\"sel\">$ta_name</td>\n</tr>\n";
	   $i++;
	}

	echo "</table>\n";
	closeDB($db);
}

function ShowTASK() {
	global $datetime;

	$db = OpenDB();
	$query = "select ta_num, pl_num, pl_prnum, ta_id, ta_name, ta_meeting, ";
	$query .= "ta_start, ta_duration, ta_level, ta_hash from task, plan ";
	$query .= "where pl_num = ta_plnum ";
	$query .= "order by pl_prnum, ta_id";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;

	if ($anz <= 0) {
	   closeDB($db);
	   Error("Esgibt keine Datens&auml;tze zum Anzeigen!");
	   return;
	}
?>
<table class="sel">
   <tr>
      <th class="sel">TaNr.</th>
      <th class="sel">PlNr.</th>
      <th class="sel">PrNr.</th>
      <th class="sel">Lfd.</th>
      <th class="sel">Level</th>
      <th class="sel">Taskname</th>
      <th class="sel">Art</th>
      <th class="sel">Startdatum</th>
      <th class="sel">Dauer in T.</th>
      <th class="sel">Hash</th>
   </tr>
<?
	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $ta_num = $data[0];
	   $pl_num = $data[1];
	   $pl_prnum = $data[2];
	   $ta_id = $data[3];
	   $ta_name = $data[4];
	   $ta_meeting = $data[5];
	   $ta_start = $data[6];
	   $ta_duration = $data[7];
	   $ta_level = $data[8];
	   $ta_hash = $data[9];

	   echo "<tr>\n<td class=\"selnum\">$ta_num</td>\n";
	   echo "<td class=\"selnum\">$pl_num</td>\n";
	   echo "<td class=\"selnum\">$pl_prnum</td>\n";
	   echo "<td class=\"selnum\">$ta_id</td>\n";
	   echo "<td class=\"selnum\">$ta_level</td>\n";
	   echo "<td class=\"sel\">$ta_name</td>\n";
	   echo "<td class=\"sel\">";

	   if ($ta_meeting == 0)
	      echo "Task";
	   else
	      echo "Meilenstein";

	   echo "</td>\n";
	   echo "<td class=\"sel\">" . gmdate($datetime, $ta_start) . "</td>\n";
	   echo "<td class=\"selnum\">$ta_duration (" . gmdate($datetime, $ta_start+$ta_duration*86400) . ")</td>\n";
	   echo "<td class=\"sel\">$ta_hash</td>\n</tr>\n";
	   $i++;
	}

	echo "</table>\n";
	closeDB($db);
}

function ShowALLOCATION() {
	global $datetime;

	$db = OpenDB();
	$query = "select al_num, ta_name, mi_nname, mi_vname, ";
	$query .= "al_hours, al_pstart, al_task from allocation, task, mitarbeiter ";
	$query .= "where ta_num = al_task and mi_num = al_ressource ";
	$query .= "order by ta_name, mi_nname, mi_vname, al_pstart desc";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;

	if ($anz <= 0) {
	   closeDB($db);
	   Error("Esgibt keine Datens&auml;tze zum Anzeigen!");
	   return;
	}
?>
<table class="sel">
   <tr>
      <th class="sel">AlNr.</th>
      <th class="sel">TaskNr.</th>
      <th class="sel">Taskname</th>
      <th class="sel">Mitarbeiter</th>
      <th class="sel">Tage</th>
      <th class="sel">Datum</th>
   </tr>
<?
	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $al_num = $data[0];
	   $ta_name = $data[1];
	   $mi_nname = $data[2];
	   $mi_vname = $data[3];
	   $al_hours = $data[4];
	   $al_pstart = $data[5];
	   $al_task = $data[6];

	   echo "<tr>\n<td class=\"selnum\">$al_num</td>\n";
	   echo "<td class=\"selnum\">$al_task</td>\n";
	   echo "<td class=\"sel\">$ta_name</td>\n";
	   echo "<td class=\"sel\">$mi_nname $mi_vname</td>\n";
	   echo "<td class=\"selnum\">" . FormatNum($al_hours, 3) . "</td>\n";
	   echo "<td class=\"sel\">" . gmdate($datetime, $al_pstart) . "</td>\n";
	   $i++;
	}

	echo "</table>\n";
	closeDB($db);
}

function ShowCALENDAR() {
	global $datetime;

	$db = OpenDB();
	$query = "select ca_num, ca_prnum, ca_minum, ca_task, ";
	$query .= "ca_writer, ca_date, ca_end, ca_title, ca_status from ";
	$query .= "calendar ";
	$query .= "order by ca_prnum, ca_date desc";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;

	if ($anz <= 0) {
	   closeDB($db);
	   Error("Es gibt keine Datens&auml;tze zum Anzeigen!");
	   return;
	}
Error("Anzahl Datens&auml;tze: $anz");
?>
<table class="sel">
   <tr>
      <th class="sel">Lfd.</th>
      <th class="sel">PrNr.</th>
      <th class="sel">Mitarbeiter</th>
      <th class="sel">Taskname</th>
      <th class="sel">Verfasser</th>
      <th class="sel">Start Datum / Zeit</th>
      <th class="sel">Ende Datum / Zeit</th>
      <th class="sel">Titel</th>
      <th class="sel">Status</th>
   </tr>
<?
	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $ca_num = $data[0];
	   $ca_prnum = $data[1];
	   $ca_minum = $data[2];
	   $ca_task = $data[3];
	   $ca_writer = $data[4];
	   $ca_date = $data[5];
	   $ca_end = $data[6];
	   $ca_title = $data[7];
	   $ca_status = $data[8];

	   if (isset($ca_minum) && $ca_minum > 0) {
	      $query = "select mi_nname, mi_vname from mitarbeiter where mi_num = $ca_minum";

	      if (!($resmi = QueryDB($db, $query))) {
	         closeDB($db);
	         return;
	      }

	      $data = fetchDB($resmi, 0);
	      $mi_nname = $data[0];
	      $mi_vname = $data[1];
	   } else {
	      $mi_nname = "";
	      $mi_vname = "";
	   }

	   $query = "select mi_nname, mi_vname from mitarbeiter where mi_num = $ca_writer";

	   if (!($reswr = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $data = fetchDB($reswr, 0);
	   $wr_nname = $data[0];
	   $wr_vname = $data[1];

	   if (isset($ca_task) && $ca_task > 0) {
	      $query = "select ta_name from task where ta_num = $ca_task";

	      if (!($resta = QueryDB($db, $query))) {
	         closeDB($db);
	         return;
	      }

	      $data = fetchDB($resta, 0);
	      $ta_name = $data[0];
	   } else
	      $ta_name = "";

	   echo "<tr>\n<td class=\"selnum\">$ca_num</td>\n";
	   echo "<td class=\"selnum\">$ca_prnum</td>\n";
	   echo "<td class=\"sel\">$mi_nname $mi_vname</td>\n";
	   echo "<td class=\"sel\">$ta_name</td>\n";
	   echo "<td class=\"sel\">$wr_nname $wr_vname</td>\n";
	   echo "<td class=\"selnum\">" . gmdate($datetime, $ca_date) . "</td>\n";
	   echo "<td class=\"selnum\">" . gmdate($datetime, $ca_end) . "</td>\n";
	   echo "<td class=\"sel\">$ca_title</td>\n\n";
	   echo "<td class=\"sel\">";

	   switch ($ca_status) {
	      case 0: echo "Erledigt"; break;
	      default: echo "Offen";
	   }

	   echo "</td>\n</tr>\n";
	   $i++;
	}

	echo "</table>\n";
	closeDB($db);
}

function WDONETime() {
	$db = OpenDB();
	$query = "select wd_num, wd_datum from wdone";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;
	$korr = 0;

	if ($anz > 0)
	   QueryDB($db, "begin");

	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $wd_num = $data[0];
	   $wd_datum = $data[1];

	   $hour = gmdate("G", $wd_datum);

	   if ($hour == 0) {
	      $i++;
	      continue;
	   }

	   $diff = 24 - $hour;
	   $wd_datum = $wd_datum + ($diff * 3600);
	   $query = "update wdone set wd_datum = $wd_datum where wd_num = $wd_num";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }

	   $korr++;
	   $i++;
	}

	if ($anz > 0)
	   QueryDB($db, "commit");

	closeDB($db);
	Error("Es wurden $korr Datens&auml;tze korrigiert!");
}

function TASKTime() {
	$db = OpenDB();
	$query = "select ta_num, ta_start from task";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;
	$korr = 0;

	if ($anz > 0)
	   QueryDB($db, "begin");

	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $ta_num = $data[0];
	   $ta_start = $data[1];

	   $hour = gmdate("G", $ta_start);
	   $min = gmdate("i", $ta_start);
	   $sec = gmdate("s", $ta_start);

	   if ($min != 0 || $sec != 0)
	      $ta_start = gmmktime(0, 0, 0, gmdate("n", $ta_start), gmdate("j", $ta_start), gmdate("Y", $ta_start));
	   else {
	      if ($hour == 0) {
	         $i++;
	         continue;
	      }

	      $diff = 24 - $hour;
	      $ta_start = $ta_start + ($diff * 3600);
	   }

	   $query = "update task set ta_start = $ta_start where ta_num = $ta_num";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }

	   $korr++;
	   $i++;
	}

	if ($anz > 0)
	   QueryDB($db, "commit");

	closeDB($db);
	Error("Es wurden $korr Datens&auml;tze korrigiert!");
}

function ALLOCATIONTime() {
	$db = OpenDB();
	$query = "select al_num, al_pstart from allocation";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;
	$korr = 0;

	if ($anz > 0)
	   QueryDB($db, "begin");

	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $al_num = $data[0];
	   $al_pstart = $data[1];

	   $hour = gmdate("G", $al_pstart);
	   $min = gmdate("i", $al_pstart);
	   $sec = gmdate("s", $al_pstart);

	   if ($min != 0 || $sec != 0)
	      $al_pstart = gmmktime(0, 0, 0, gmdate("n", $al_pstart), gmdate("j", $al_pstart), gmdate("Y", $al_pstart));
	   else {
	      if ($hour == 0) {
	         $i++;
	         continue;
	      }

	      $diff = 24 - $hour;
	      $al_pstart = $al_pstart + ($diff * 3600);
	   }

	   $query = "update allocation set al_pstart = $al_pstart where al_num = $al_num";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }

	   $korr++;
	   $i++;
	}

	if ($anz > 0)
	   QueryDB($db, "commit");

	closeDB($db);
	Error("Es wurden $korr Datens&auml;tze korrigiert!");
}

function CALENDARTime() {
	$db = OpenDB();
	$query = "select ca_num, ca_date, ca_end from calendar";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$anz = numrowsDB($result);
	$i = 0;
	$korr = 0;

	if ($anz > 0)
	   QueryDB($db, "begin");

	while ($i < $anz) {
	   $data = fetchDB($result, $i);
	   $ca_num = $data[0];
	   $ca_date = $data[1];
	   $ca_end = $data[2];

	   $hour = gmdate("G", $ca_date);
	   $min = gmdate("i", $ca_date);
	   $sec = gmdate("s", $ca_date);

	   if ($min != 0 || $sec != 0)
	      $ca_date = gmmktime(0, 0, 0, gmdate("n", $ca_date), gmdate("j", $ca_date), gmdate("Y", $ca_date));
	   else {
	      if ($hour == 0) {
	         $i++;
	         continue;
	      }

	      $diff = 24 - $hour;
	      $ca_date = $ca_date + ($diff * 3600);
	   }

	   $hour = gmdate("G", $ca_end);
	   $min = gmdate("i", $ca_end);
	   $sec = gmdate("s", $ca_end);

	   if ($min != 0 || $sec != 0)
	      $ca_end = gmmktime(0, 0, 0, gmdate("n", $ca_end), gmdate("j", $ca_end), gmdate("Y", $ca_end));
	   else {
	      if ($hour == 0) {
	         $i++;
	         continue;
	      }

	      $diff = 24 - $hour;
	      $ca_end = $ca_end + ($diff * 3600);
	   }

	   $query = "update calendar set ca_date = $ca_date, ca_end = $ca_end where ca_num = $ca_num";

	   if (!QueryDB($db, $query)) {
	      QueryDB($db, "rollback");
	      closeDB($db);
	      return;
	   }

	   $korr++;
	   $i++;
	}

	if ($anz > 0)
	   QueryDB($db, "commit");

	closeDB($db);
	Error("Es wurden $korr Datens&auml;tze korrigiert!");
}

function Auswahl() {
	global $menu;

	$nav = "headline=26&menu=$menu";
	echo "<table class=\"indent\"><tr><td colspan=2>Tabelle WDONE</td></tr>\n";
	echo "<tr><td>\n";
	ButtonLink("WDONE", "dbview.php", "$nav&func=WDONE");
	echo "</td><td>\n";
	ButtonLink("WDONE Zeitkorrektur", "dbview.php", "$nav&func=WDONETIME");
	echo "</td></tr><tr><td colspan=2>Tabelle TASK</td></tr><tr><td>\n";
	ButtonLink("TASK", "dbview.php", "$nav&func=TASK");
	echo "</td><td>";
	ButtonLink("TASK Zeitkorrektur", "dbview.php", "$nav&func=TASKTIME");
	echo "</td></tr><tr><td colspan=2>Tabelle ALLOCATION</td></tr><tr><td>\n";
	ButtonLink("ALLOCATION", "dbview.php", "$nav&func=ALLOCATION");
	echo "</td><td>";
	ButtonLink("ALLOCATION Zeitkorrektur", "dbview.php", "$nav&func=ALLOCATIONTIME");
	echo "</td></tr><tr><td colspan=2>Tabelle CALENDAR</td></tr><tr><td>\n";
	ButtonLink("CALENDAR", "dbview.php", "$nav&func=CALENDAR");
	echo "</td><td>";
	ButtonLink("CALENDAR Zeitkorrektur", "dbview.php", "$nav&func=CALENDARTIME");
	echo "</td></tr></table>\n";
}

$func = $_REQUEST['func'];
$done = false;

if (!isset($func)) {
   Auswahl();
   $done = true;
}

if ($func == "WDONE") {
   ShowWDONE();
   $done = true;
}

if ($func == "WDONETIME")
   WDONETime();

if ($func == "TASK") {
   ShowTASK();
   $done = true;
}

if ($func == "TASKTIME")
   TASKTime();

if ($func == "ALLOCATION") {
   ShowALLOCATION();
   $done = true;
}

if ($func == "ALLOCATIONTIME")
   ALLOCATIONTime();

if ($func == "CALENDAR") {
   ShowCALENDAR();
   $done = true;
}

if ($func == "CALENDARTIME")
   CALENDARTime();

if (!$done)
   Auswahl();

require_once('footer.inc');
?>
